// pages/post/create.js
const app = getApp()

Page({
  data: {
    content: '',
    images: [],
    isAnonymous: false,
    privacy: 'public', // public, private, matched
    showEmoji: false,
    showPrivacy: false,
    autoFocus: false,
    currentEmojiTab: 'all', // recent, all
    recentEmojis: [],
    emojis: [
      '😀', '😃', '😄', '😁', '😆', '😅', '😂', '🤣',
      '😊', '😇', '🙂', '🙃', '😉', '😌', '😍', '🥰',
      '😘', '😗', '😙', '😚', '😋', '😛', '😝', '😜',
      '🤪', '🤨', '🧐', '🤓', '😎', '🤩', '🥳', '😏',
      '😒', '😞', '😔', '😟', '😕', '🙁', '☹️', '😣',
      '😖', '😫', '😩', '🥺', '😢', '😭', '😤', '😠',
      '😡', '🤬', '🤯', '😳', '🥵', '🥶', '😱', '😨',
      '😰', '😥', '😓', '🤗', '🤔', '🤭', '🤫', '🤥',
      '😶', '😐', '😑', '😬', '🙄', '😯', '😦', '😧',
      '😮', '😲', '🥱', '😴', '🤤', '😪', '😵', '🤐',
      '🥴', '🤢', '🤮', '🤧', '😷', '🤒', '🤕', '🤑',
      '🤠', '😈', '👿', '👹', '👺', '🤡', '💩', '👻',
      '💀', '☠️', '👽', '👾', '🤖', '🎃', '😺', '😸',
      '😹', '😻', '😼', '😽', '🙀', '😿', '😾'
    ]
  },

  onLoad() {
    this.checkAuth();
    // 延迟设置焦点，确保页面渲染完成
    setTimeout(() => {
      this.setData({
        autoFocus: true
      });
    }, 300);
  },

  onReady() {
    // 页面渲染完成后尝试激活键盘
    setTimeout(() => {
      this.setData({
        autoFocus: true
      });
    }, 500);
  },

  // 检查授权
  checkAuth() {
    if (!app.globalData.userInfo) {
      wx.redirectTo({
        url: '/pages/login/login'
      });
    }
  },

  // 返回上一页
  goBack() {
    wx.navigateBack();
  },

  // 内容输入
  onContentInput(e) {
    const content = e.detail.value;
    this.setData({
      content: content
    });
  },

  // 输入框获得焦点
  onInputFocus() {
    // 收起其他面板
    this.setData({
      showEmoji: false,
      showPrivacy: false
    });
  },

  // 发布动态
  publishPost() {
    const { content, images, isAnonymous, privacy } = this.data;
    
    if (!content.trim()) {
      wx.showToast({
        title: '请输入内容',
        icon: 'none'
      });
      return;
    }

    // 显示发布中状态
    wx.showLoading({
      title: '发布中...',
    });

    // 模拟发布过程
    setTimeout(() => {
      wx.hideLoading();
      
      // 发布成功
      wx.showToast({
        title: '发布成功',
        icon: 'success',
        duration: 2000
      });

      // 返回上一页
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);

    }, 1500);
  },

  // 添加图片
  addImage() {
    const that = this;
    wx.chooseMedia({
      count: 9 - that.data.images.length,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        const tempFiles = res.tempFiles;
        const newImages = tempFiles.map(file => file.tempFilePath);
        that.setData({
          images: [...that.data.images, ...newImages]
        });
      },
      fail(err) {
        console.error('选择图片失败:', err);
        wx.showToast({
          title: '选择图片失败',
          icon: 'none'
        });
      }
    });
  },

  // 删除图片
  deleteImage(e) {
    const index = e.currentTarget.dataset.index;
    const images = [...this.data.images];
    images.splice(index, 1);
    this.setData({
      images: images
    });
  },

  // 打开相册
  openAlbum() {
    this.addImage();
  },

  // 打开相机
  openCamera() {
    const that = this;
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        const tempFiles = res.tempFiles;
        const newImages = tempFiles.map(file => file.tempFilePath);
        that.setData({
          images: [...that.data.images, ...newImages]
        });
      },
      fail(err) {
        console.error('拍照失败:', err);
        wx.showToast({
          title: '拍照失败',
          icon: 'none'
        });
      }
    });
  },

  // 切换表情面板
  toggleEmoji() {
    this.setData({
      showEmoji: !this.data.showEmoji,
      showPrivacy: false
    });
  },

  // 选择表情
  selectEmoji(e) {
    const emoji = e.currentTarget.dataset.emoji;
    const content = this.data.content + emoji;
    this.setData({
      content: content
    });
    
    // 添加到最近使用表情
    this.addToRecentEmojis(emoji);
  },

  // 添加到最近使用表情
  addToRecentEmojis(emoji) {
    let recentEmojis = [...this.data.recentEmojis];
    
    // 如果表情已经在最近使用中，先移除
    recentEmojis = recentEmojis.filter(e => e !== emoji);
    
    // 添加到开头
    recentEmojis.unshift(emoji);
    
    // 限制最近使用表情数量
    if (recentEmojis.length > 16) {
      recentEmojis = recentEmojis.slice(0, 16);
    }
    
    this.setData({
      recentEmojis: recentEmojis
    });
  },

  // 切换表情标签
  switchEmojiTab(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      currentEmojiTab: tab
    });
  },

  // 切换匿名状态
  toggleAnonymous() {
    this.setData({
      isAnonymous: !this.data.isAnonymous
    });
  },

  // 显示隐私选项
  showPrivacyOptions() {
    this.setData({
      showPrivacy: !this.data.showPrivacy,
      showEmoji: false
    });
  },

  // 设置隐私选项
  setPrivacy(e) {
    const type = e.currentTarget.dataset.type;
    this.setData({
      privacy: type,
      showPrivacy: false
    });
  },

  // 隐藏隐私选项面板
  hidePrivacyPanel() {
    this.setData({
      showPrivacy: false
    });
  },

  // 计算隐私选项显示文字
  getPrivacyText() {
    const { privacy } = this.data;
    switch (privacy) {
      case 'public':
        return '公开';
      case 'private':
        return '私密';
      case 'matched':
        return '仅配对';
      default:
        return '公开';
    }
  }
})
